package util;

import java.sql.*;
/*
查询某条数据
 */
public class JDBCUtil_5 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //链接驱动Connection对象
            conn = DriverManager.getConnection("jdbc:mysql:///sanqiansql?serverTimezone=GMT","root","shaozhaoming");
            //定义sql语句
            String sql = "select * from demo";
            //获取执行sql的statement对象
            stmt = conn.createStatement();
            //执行sql语句
            rs = stmt.executeQuery(sql);
            //移动游标
            rs.next();
            //获取数据
            int id = rs.getInt(1);
            String name = rs.getString("name");
            System.out.println(id+"------"+name);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stmt!=null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
